package com.carryking.smart.rpc.spring;

import com.carryking.smart.rpc.discover.DiscoverServiceFactory;
import com.carryking.smart.rpc.entity.DiscoverConfigEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by jincarry on 2018/1/11.
 */
@Slf4j
public class ClientSpringInitListener implements ApplicationListener<ContextRefreshedEvent> {

    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        //初始化服务中心
        proccessDiscoverService(contextRefreshedEvent.getApplicationContext());
    }

    private void proccessDiscoverService(ApplicationContext context) {

        DiscoverConfigEntity configEntity = context.getBean(DiscoverConfigEntity.class);

        if (configEntity == null) {
            throw new RuntimeException("初始化服务注册中心失败");
        }
        DiscoverServiceFactory.init(configEntity);
    }
}
